<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/default/easyui.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/icon.css}">
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.easyui.min.js}"></script>
    <script type="text/javascript">
        //工具栏
        var toolbar = [{
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                $('#dd').dialog({
                    title: "添加角色"
                });
                $("#id").parent().hide();
                $("#name").textbox('setValue', '');
                $('#addOrModify').val("add");
                clearTree($("#ah"));
                $('#dd').dialog('open');
            }
        }, '-', {
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                $('#dd').dialog({
                    title: "修改角色"
                });
                let selected = $('#rolelist').datagrid('getSelected');
                if (!selected) {
                    alert("请选择待修改的角色");
                } else {
                    $('#addOrModify').val("modify");
                    $("#id").parent().show();
                    clearTree($("#ah"));
                    $("#id").textbox("setValue", selected.id);
                    $("#name").textbox("setValue", selected.name);
                    let permissions = selected.permissions;
                    for (const permission of permissions) {
                        let node = $('#ah').tree('find', permission.id);
                        $('#ah').tree('check', node.target);
                    }
                    $('#dd').dialog('open');
                }
            }
        }, '-', {
            text: '删除',
            iconCls: 'icon-cancel',
            handler: function () {
                let selected = $('#rolelist').datagrid('getSelected');
                if (!selected) {
                    alert("请选择待删除的角色");
                } else {
                    if (confirm("确认删除?")) {
                        $.ajax({
                            url: "/manager/role/remove",
                            data: {
                                "id": selected.id,
                            },
                            type: "post",
                            dataType: "json",
                            success: function (data) {
                                if (data.status !== 2001) {
                                    alert(data.msg);
                                } else {
                                    $('#rolelist').datagrid('reload');
                                }
                            }
                        });
                    }
                }
            }
        }];
        $(function () {
            $("#rolenameKey").val("");
            $('#rolelist').datagrid({
                title: '角色列表',
                iconCls: 'icon-save',
                url: "/manager/role/findAll",
                columns: [
                    [
                        {field: 'id', title: '角色ID', width: 100, align: 'center'},
                        {field: 'name', title: '角色名称', width: 200, align: 'center'},
                        {
                            field: 'permissions',
                            title: '角色拥有的权限[权限名 | 权限URL | 模块名 | 操作名 ]',
                            width: 700,
                            align: 'center',
                            formatter: format
                        }
                    ]
                ],
                pagination: true,
                toolbar: toolbar,
                pageSize: 5,
                singleSelect: true,
                pageList: [5, 10, 20]
            });
            //初始化查询栏
            $("#submit_search").linkbutton({iconCls: 'icon-search', plain: true})
                .click(function () {
                    $('#rolelist').datagrid({
                        queryParams: {
                            "rolenameKey": $("#rolenameKey").val(),
                        }
                    });   //点击搜索
                });

            //初始化添加修改窗口
            $('#dd').dialog({
                width: 700,
                height: 500,
                closed: true,
                cache: false,
                href: '',
                modal: true,
                iconCls: 'icon-save',
                buttons: [{
                    text: '提交',
                    iconCls: 'icon-ok',
                    handler: function () {
                        let url = $("#addOrModify").val() === "add" ? "/manager/role/add" : "/manager/role/modify";
                        let nodes = $('#ah').tree('getChecked',['checked','indeterminate']);
                        let permissions = [];
                        for (let i = 0; i < nodes.length; i++) {
                            permissions.push(nodes[i].id);
                        }
                        $.ajax({
                            url: url,
                            data: {
                                "permissions": permissions,
                                "name": $("#name").val(),
                                "id": $("#id").val(),
                            },
                            type: "post",
                            dataType: "json",
                            success: function (data) {
                                if (data.status !== 2001) {
                                    $('#dd').dialog('close');
                                    alert(data.msg);
                                } else {
                                    $('#dd').dialog('close');
                                    $('#rolelist').datagrid('reload');
                                }
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    handler: function () {
                        $('#dd').dialog('close');
                    }
                }]
            });

            //tree选项框初始化
            $("#ah").tree({
                url: "/manager/permission/findPermissiontree",
                method: 'post',
                animate: true,
                checkbox: true
            });

        });

        //列格式化方法
        function format(val, row) {
            console.log(val)
            let sel = "<select name='search_type'>";
            for (let i = 0; i < val.length; i++) {
                let parent = val[i].parentPermission;
                let parentname;
                if (!parent) {
                    parentname = "根权限";
                } else {
                    parentname = parent.name;
                }
                let url = val[i].url;
                if (!url){
                    url = "无";
                }
                sel += "<option>" + val[i].name + "|" + url + "|" + parentname + "|" +
                    val[i].sname +
                    "</option>"
            }

            return sel;
        }

        function clearTree(_tree) {
            const roots = _tree.tree('getRoots');
            for (let i = 0; i < roots.length; i++) {
                let node = _tree.tree('find', roots[i].id);
                _tree.tree('uncheck', node.target);
            }
        }
    </script>
</head>
<body>
<form name="searchform" method="post" action="" id="searchform">
    <td width="70" height="30"><strong>角色查询：</strong></td>
    <td height="30">
        <input type="text" name="rolenameKey" id="rolenameKey" size=20 placeholder="角色名称"/>
        <a id="submit_search">搜索</a>
    </td>
</form>
<table id="rolelist"></table>
<div id="dd">
    <div align="center" style="margin-top: 30px">
        <form id="ff" method="post">
            <input type="hidden" id="addOrModify"/>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="id" id="id" style="width:50%" data-options="label:'角色ID:'"
                       readonly="readonly"/>
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="name" id="name" style="width:50%"
                       data-options="label:'角色名称:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <table style="width:48%">
                    <td valign="top" style="width:50px">
                        权限:
                    </td>
                    <td>
                        <ul id="ah"></ul>
                    </td>
                </table>
            </div>
        </form>
    </div>
</div>
</body>
</html>